<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/
	
require_once 'PHPUnit/Framework.php';
require_once 'autoload.php';

class TestDownload extends PHPUnit_Framework_TestCase {

	public function test_prepare() {
		$expected_item = array('body' => 'body_content');
		$expected_inputs = array();
		$expected_desqlized_body = 'desqlized_body';
		$expected_checkbox = 'checkbox';
		$expected_text_input = 'text_input';

		$page = new Page();

		$download = new Download($page);

		$mock_creator = $this->getMock('FormCreator');
		$mock_creator->expects($this->once())
				->method('input_checkbox')
				->with("Bestand wijzigen", "change_file", "", 
					"", "document.itemform.submit()")
				->will($this->returnValue($expected_checkbox));
		$mock_creator->expects($this->once())
				->method('input_text')
				->with("Beschrijving", "body", $expected_desqlized_body)
				->will($this->returnValue($expected_text_input));
		$download->set_form_creator($mock_creator);

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_desqlize')
				->with($expected_item['body'])
				->will($this->returnValue($expected_desqlized_body));
		$download->set_db_accessor($mock_db_access);

		$actual = $download->prepare(false, $expected_item, $expected_inputs);
		$this->assertEquals($expected_checkbox, $actual[0]);
		$this->assertEquals($expected_text_input, $actual[1]);
	}

	public function test_prepare_with_new() {
		$expected_item = array('body' => 'body_content');
		$expected_inputs = array();
		$expected_desqlized_body = 'desqlized_body';
		$expected_download = 'download';
		$expected_text_input = 'text_input';

		$page = new Page();

		$download = new Download($page);

		$mock_creator = $this->getMock('FormCreator');
		$mock_creator->expects($this->once())
				->method('input_file')
				->with("Bestand", "downloadfile")
				->will($this->returnValue($expected_download));
		$mock_creator->expects($this->once())
				->method('input_text')
				->with("Beschrijving", "body", $expected_desqlized_body)
				->will($this->returnValue($expected_text_input));
		$download->set_form_creator($mock_creator);

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_desqlize')
				->with($expected_item['body'])
				->will($this->returnValue($expected_desqlized_body));
		$download->set_db_accessor($mock_db_access);

		$actual = $download->prepare(true, $expected_item, $expected_inputs);
		$this->assertEquals($expected_download, $actual[0]);
		$this->assertEquals($expected_text_input, $actual[1]);
	}

	public function test_prepare_with_post_change_file() {
		$expected_item = array('body' => 'body_content');
		$expected_inputs = array();
		$expected_desqlized_body = 'desqlized_body';
		$expected_download = 'download';
		$expected_text_input = 'text_input';
		$expected_checkbox = 'checkbox';

		$_POST['change_file'] = true;
		$page = new Page();

		$download = new Download($page);

		$mock_creator = $this->getMock('FormCreator');
		$mock_creator->expects($this->once())
				->method('input_checkbox')
				->with("Bestand wijzigen", "change_file", "", 
					"", "document.itemform.submit()")
				->will($this->returnValue($expected_checkbox));
		$mock_creator->expects($this->once())
				->method('input_file')
				->with("Bestand", "downloadfile")
				->will($this->returnValue($expected_download));
		$mock_creator->expects($this->once())
				->method('input_text')
				->with("Beschrijving", "body", $expected_desqlized_body)
				->will($this->returnValue($expected_text_input));
		$download->set_form_creator($mock_creator);

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_desqlize')
				->with($expected_item['body'])
				->will($this->returnValue($expected_desqlized_body));
		$download->set_db_accessor($mock_db_access);

		$actual = $download->prepare(false, $expected_item, $expected_inputs);

		$this->assertEquals($expected_checkbox, $actual[0]);
		$this->assertEquals($expected_download, $actual[1]);
		$this->assertEquals($expected_text_input, $actual[2]);
	}

	public function test_get_edit_body() {
		$expected_query = 'query';
		$expected_result = 'result';
		$expected_body = 'corpse';
		$expected_array = array('body' => $expected_body);
		$expected_id = 586;

		$page = new Page();
		$download = new Download($page, $expected_id);

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method("db_query_select")
				->with(array(array('*')), array("DOWNLOAD"), "item_id = $expected_id")
				->will($this->returnValue($expected_query));
		$mock_db_access->expects($this->once())
				->method("db_do_query")
				->with($expected_query)
				->will($this->returnValue($expected_result));
		$mock_db_access->expects($this->once())
				->method("db_fetch_array")
				->with($expected_result)
				->will($this->returnValue($expected_array));
		$download->set_db_accessor($mock_db_access);

		$actual = $download->get_edit_body();

		$this->assertEquals($expected_body, $actual);
	}

	public function test_check_content_with_change_file() {
		$expected_body_value = 'body value';
		$expected_change_file_value = true;
		$expected_values = array('body' => $expected_body_value, 
				'change_file' => $expected_change_file_value);

		$page = new Page();

		$download = new Download($page);
		$mock_checker = $this->getMock('InputChecker');
		$mock_checker->expects($this->at(0))
				->method('check_tags_advanced')
				->with($expected_body_value, 'body')
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(1))
				->method('check_tag_closures')
				->with($expected_body_value)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(2))
				->method('check_exists')
				->with($expected_body_value, 'body')
				->will($this->returnValue(true));
		$download->set_input_checker($mock_checker);

		$actual = $download->check_content($expected_values, $expected_new);
		$this->assertFalse($actual);
	}

	public function test_check_content_with_not_change_file() {
		$expected_body_value = 'body value';
		$expected_change_file_value = false;
		$expected_values = array('body' => $expected_body_value, 
				'change_file' => $expected_change_file_value);

		$page = new Page();

		$download = new Download($page);
		$mock_checker = $this->getMock('InputChecker');
		$mock_checker->expects($this->at(0))
				->method('check_tags_advanced')
				->with($expected_body_value, 'body')
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(1))
				->method('check_tag_closures')
				->with($expected_body_value)
				->will($this->returnValue(true));
		$mock_checker->expects($this->at(2))
				->method('check_exists')
				->with($expected_body_value, 'body')
				->will($this->returnValue(true));
		$download->set_input_checker($mock_checker);

		$actual = $download->check_content($expected_values, $expected_new);
		$this->assertTrue($actual);
	}

	public function test_derive_info() {
		$expected_post_body_stripped = 'Post body of less than fifty characters.';
		$expected_post_body = "<html>$expected_post_body_stripped</html>";
		$_POST['body'] = $expected_post_body;

		$download = new FakeDownload($page);
		$actual = $download->test_derive_info();
		$this->assertEquals($expected_post_body_stripped, $actual);
	}

	public function test_derive_info_with_long_post_body() {
		$expected_post_body_stripped = 'Post body of more than fifty characters. We may need a couple more sentences for that. Like so, would that be enough? Or is one more even better. Oh whatever, I think this will do just fine.';
		$expected_post_body = "<html>$expected_post_body_stripped</html>";
		$_POST['body'] = $expected_post_body;

		$download = new FakeDownload($page);
		$actual = $download->test_derive_info();
		$this->assertEquals(substr($expected_post_body_stripped, 0, 50) . '&hellip;', $actual);
	}
}

class FakeDownload extends Download {
	
	public function test_derive_info() {
		return parent::derive_info();
	}
}
?>
